/**
 * # Hapi.js
 *
 * This is a configuration for Hapi
 *
 * Note that Hapi is configuration over coding
 *
 * There's no coding here!!
 */
'use strict';

/**
* ## Imports
*
*/
var
    Config = require('../config'),
    //Hapi itself
    Hapi = require('hapi'),
    // the authentication strategy
    JwtAuth = require('../auth/jwt-strategy'),
    // kind of like underscore, but specific to Hapi
    Hoek = require('hoek'),
    // the routes we'll support
    Routes = require('./routes'),
    // the view, mainly for reset password
    Views = require('./views');

const Good = require('good');
const hapiAuthJwt = require('hapi-auth-jwt2');

var internals = {};

//The real Hapi server!
internals.server = new Hapi.Server();

//Setup the connection for the environment
internals.server.connection({
  port: process.env.OPENSHIFT_NODEJS_PORT || Config.hapi.port,
  address: process.env.OPENSHIFT_NODEJS_IP || Config.hapi.ip
});

// https://hapijs.com/tutorials#using-plugins
internals.server.register({
  register: Good,
  options: {
    reporters: {
      console: [
        {
          module: 'good-squeeze',
          name: 'Squeeze',
          args: [{
            response: '*',
            log: '*'
          }]
        },
        {
          module: 'good-console'
        },
        'stdout'
      ]
    }
  }
});

// https://github.com/dwyl/hapi-auth-jwt2#example
internals.server.register(hapiAuthJwt, function (err) {
  if (err) throw err;
});

// configure jwt strategy
JwtAuth.setStrategy(internals.server);

//setup views for resetpassword
Views.init(internals.server);

// set routes
Routes.init(internals.server);

module.exports = internals.server;
